package org.dizem.groupon.action;

import com.opensymphony.xwork2.Preparable;
import org.dizem.groupon.model.domain.LinkEntity;
import org.dizem.groupon.service.LinkService;
import org.dizem.util.Pager;

import java.util.List;

/**
 * User: dizem
 * Time: 11-7-19 上午11:47
 */
public class LinkAction extends BaseAction implements Preparable {

	private LinkService service;
	private List<LinkEntity> links;
	private int id;
	private LinkEntity link;

	public LinkService getService() {
		return service;
	}

	public void setService(LinkService service) {
		this.service = service;
	}

	public List<LinkEntity> getLinks() {
		return links;
	}

	public void setLinks(List<LinkEntity> links) {
		this.links = links;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public LinkEntity getLink() {
		return link;
	}

	public void setLink(LinkEntity link) {
		this.link = link;
	}

	public void prepare() throws Exception {
		links = service.loadAll();
	}

	public String listLink() throws Exception {
		if (this.getRecordCount() < 0) {
			setRecordCount(service.getCount());
		}
		if (getPage() == null) {
			setPage(1);
		}
		Pager p = new Pager(getPage(), getRecordCount(), getPageSize());
		setPageCount(p.getPageCount());
		setPageUrl("listLink");
		setPage(p.getPage());

		links = service.load(p.getStartPage(), this.getPageSize());
		return SUCCESS;
	}

	public String edit() throws Exception {
		link = service.getById(id);
		return SUCCESS;
	}

	public String update() throws Exception {
		service.update(link);
		return SUCCESS;
	}

	public String add() throws Exception {
		service.save(link);
		return SUCCESS;
	}

	public String delete() throws Exception {
		LinkEntity link = service.getById(id);
		if (link != null)
			service.delete(link);
		return SUCCESS;
	}
}
